$(function(){

    //判断是否是推荐过来的
    var args = getUrlParams();
    if(args.r){
        $.cookie('refererMobile',args.r);
    }

    //头部信息
    var h = new Vue({
        el: '#header-information',
        data:{
            // 区块行情
            total: "", //总价值
            totalDay: "", //24H成交价
            blockNum:"" , //总币种数量
            upCount:"" ,  //成交涨跌
            // exponent: 8883.80, //指数
            currName: "", //当前指数名
            currValue: "", //当前指数量
            exponent: [{name:"",num:""}], //指数列表
            //导航条区块货币简介
            blockProfile:[],
        },
        methods:{
            //币种列表
            goCurrency: function(){
                location.href="currency.html"
            },
            //币种详情
            goCurrencyProfile: function(event){
                var id = event.currentTarget.getAttribute('value');
                var symbol = event.currentTarget.getAttribute('symbol');
                // location.href = 'currencyProfile.html?id='+id+'&content='+symbol;
                window.open('currencyProfile.html?id='+id+'&content='+symbol);
            },
            getIndexValue:function(event){
                var id = event.currentTarget.getAttribute('value');
                this.currName = this.exponent[id].indexName;
                this.currValue = this.exponent[id].indexValue;
            }
        },
        mounted:function(){
            this.$nextTick(function(){
                getHomeSummary({element:h});
            });
        },
    });

    //货币信息显示
    var c = new Vue({
        el: '#block-information',
        data:{
            temp: "",
            //近期风口
            tuyereBlock:[], //location定位item是左中右哪个，fontColor是字体颜色
            idList : ['performance','every-buy','sestem'],
            totalNum: 0, //近期风口总数
            isShow: false,
            lists : [
                {
                    displayName:"",  //名称
                    desc: "" ,  //简称
                    block:[],
                },
                {
                    displayName:"",  //名称
                    desc: "" ,  //简称
                    block:[],
                },
                {
                    displayName:"",  //名称
                    desc: "" ,  //简称
                    block:[],
                },
            ],

        },
        methods: {
            getItemLocation: function(id){
                var str = '';
                var num = id % 3;
                switch(num){
                    case 0:
                        str = 'right-item';
                        break;
                    case 1:
                        str = 'left-item';
                        break;
                    case 2:
                        str = 'middle-item';
                }
                return str;
            },
            hightShow: function(type,event){
                switch(type){
                    case 0:
                        event.currentTarget.setAttribute('src','image/zuoActive.png');
                        break;
                    case 1:
                        event.currentTarget.setAttribute('src','image/youActive.png');
                        break;
                }

            },
            remHightShow: function(type,event){
                switch(type){
                    case 0:
                        event.currentTarget.setAttribute('src','image/zuo.png');
                        break;
                    case 1:
                        event.currentTarget.setAttribute('src','image/you.png');
                        break;
                }

            },
            goCurrencyProfile: function(event){
                var id = event.currentTarget.getAttribute('value');
                var symbol = event.currentTarget.getAttribute('symbol');
                window.open('currencyProfile.html?id='+id+'&content='+symbol);
            },
            goCurrency: function(event){
                var id = event.currentTarget.getAttribute('value');
                location.href = 'currency.html?id='+id;
                window.open('currency.html?id='+id);
            },
            setTuyereWidth: function () {

                if($(window).width() < 840){
                    var marginWidth = ($('.item-group').width() - 236 ) /2;
                    var tuyereWidth = this.tuyereBlock.length  * ( 236 + marginWidth * 2);
                    $('.china-item .item').css({'margin-left':marginWidth, 'margin-right':marginWidth});

                    console.log(tuyereWidth+20);

                    // var boxWidth = $('.china-item').eq(0).outerWidth(true);
                    $('#tuyere').find('.item-box').width(tuyereWidth+6);
                }else{
                    var boxWidth = $('.china-item').eq(0).width();
                    var tuyereWidth = Math.ceil(this.tuyereBlock.length / 3) * boxWidth;
                    $('#tuyere').find('.item-box').width(tuyereWidth);
                }

            },
            //计算滚动内容的宽度；
            setOtherWidth: function () {
                var boxWidth = $('.china-item').eq(0).width();
                if($(window).width() < 840){
                    var buyWidth = Math.ceil(this.lists[1].block.length ) * boxWidth;
                    var performanceWidth = Math.ceil(this.lists[0].block.length ) * boxWidth;
                    var sestemWidth = Math.ceil(this.lists[2].block.length) * boxWidth;

                    // var marginWidth = ($('.item-group').width() - 236 ) /2;
                    // $('.china-item .item').css({'margin-left':marginWidth, 'margin-right':marginWidth});
                    var marginWidth = ($('.item-group').width() - 236 ) /2;
                    $('.china-item .item').css({'margin-left':marginWidth, 'margin-right':marginWidth});

                    $('#every-buy').find('.item-box').width(buyWidth+6);
                    $('#performance').find('.item-box').width(performanceWidth+6);
                    $('#sestem').find('.item-box').width(sestemWidth+6);
                }else{
                    var buyWidth = Math.ceil(this.lists[1].block.length / 3) * boxWidth;
                    var performanceWidth = Math.ceil(this.lists[0].block.length / 3) * boxWidth;
                    var sestemWidth = Math.ceil(this.lists[2].block.length / 3) * boxWidth;

                    $('#every-buy').find('.item-box').width(buyWidth);
                    $('#performance').find('.item-box').width(performanceWidth);
                    $('#sestem').find('.item-box').width(sestemWidth);
                }


            },
            //滑动函数，e为传入参数
            blockSilder: function(sign,event){
                var obj = $(event.currentTarget).parent().nextAll('.item-group').eq(0);

                //屏幕小于840的，移动距离为一个ITEM大小
                if($(window).width() < 840){
                    var width = parseFloat($('.china-item .item').outerWidth(true)) ;
                }else{
                    var width = parseFloat(obj.width());
                }

                if(sign == 'next'){
                    var left = parseFloat(obj.children().css('left')) - width;
                    console.log(Math.abs(left) + '  ---   '+ obj.children().width());
                    if(Math.abs(left) < obj.children().width()){
                        obj.find('.item-box').eq(0).animate({left: left},300);
                    }else{
                        obj.find('.item-box').eq(0).animate({left:0},300);
                    }
                }else{
                    var left = parseFloat(obj.children().css('left')) + width;
                    if(left <=0){
                        obj.find('.item-box').eq(0).animate({left: left},300);
                    }
                }
            },
            //鼠标移入
            toEnter:function(event){
                var top = parseInt($(event.currentTarget).css('margin-top'));
                var left = parseInt($(event.currentTarget).css('margin-left'));
                $(event.currentTarget).animate({
                    'top':'-4px',
                    // 'left':'-2px',
                },200);
            },
            //鼠标移出
            toLeave:function(event){
                var top = parseInt($(event.currentTarget).css('margin-top'));
                var left = parseInt($(event.currentTarget).css('margin-left'));
                $(event.currentTarget).animate({
                    'top':'0',
                    // 'left':'0',
                    // 'margin-left': left +5,
                    // 'margin-top': top+5,
                },100);
            },
        },
        mounted:function(){
           this.$nextTick(function(){

            //风口概念
           getSummaryConcepts({element:c});

           //其他模块
           getHomeModules({element:c});


           });

        },
    });

    //右侧快讯，公告
    var n = new Vue({
        el: '#news-information',
        data:{
            totalNum:"",  //快讯总条数
            quickBlock: [],  //快讯列表
            noticeBlock:[],  //公告列表
            proBlock:[],  //涨幅榜列表
            flowBlock:[], //资金流入列表
            pageSize: 10, //公告
            pageNum: 1, //公告
            valueType: 0 , //涨跌标志 0 涨   1跌
            directionType: 1, //资金流向  0 流入 1 流出
            downImg: 'image/xiala.png',
            timeType: '1d', //
            updateNum:0, //快讯更新条数
            oldTimestamp:0, //记录上次更新时间戳

        },
        components:{
            'notice-content': noticeComponent,
            'up-down-content': upDownComponent,
        },
        methods:{
            //首页右侧 公告和快讯的 更多按钮链接
            goPage: function(event){
               var value = parseInt($('#news-information').find('.message-box .tab-content .active').attr('value'));
               switch(value){
                   case 1:
                       location.href = 'information.html';
                       break;
                   case 2:
                       location.href = 'bourseNotice.html';
                       break;
               }
            },
            //币种详情
            goCurrencyProfile:function(event){
                var id = event.currentTarget.getAttribute('value');
                var symbol = event.currentTarget.getAttribute('symbol');
                location.href = 'currencyProfile.html?id='+id+'&content='+symbol;
                // window.open("currencyProfile.html?id="+id);
            },
            changeSort: changeSort,
            //共享系列
            toShare:showShare,
            //根据类型选择对应的分享方式
            toShareType:shareType,
            //鼠标移出关闭共享框
            closeShare: closeShare,
            //利好利空
            setUpDown:function(event){
                var id = event.currentTarget.getAttribute('value'); //文章ID
                var type = event.currentTarget.getAttribute('type');  //利好 0    利空 1
                var index = event.currentTarget.getAttribute('index'); //数组索引
                //更新利好利空
                updateUpDown({url:'zan' ,infoType:'1' , flag:type,id: id,element: n.quickBlock[index], currEvent: event.currentTarget});
            },
            resetScroll: function(){
                document.getElementById('news-information').getElementsByClassName('tab-content')[0].scrollTop = 0;
            },
            //右侧涨幅榜和流入榜下方三角箭头的切换
            toNextPage: function(){
                var obj = $('#main .body .right .ranking-box .tab-content');
                if(obj.attr('value')=='0'){
                    obj.attr('value',1).animate({height:600},500);
                    this.downImg = 'image/shangla.png';
                }else{
                    obj.attr('value',0).animate({height:330},500);
                    this.downImg = 'image/xiala.png';
                }
            },
            // 显示涨跌幅切换按钮
            showUpDown:function(event){
                var type = event.currentTarget.getAttribute('value');
                if(type == '0'){
                    $('.ranking-box .btn-group').addClass('hidden');
                }else{
                    $('.ranking-box .btn-group').removeClass('hidden');
                }
            },
            //更新公告
            toUpdate: function(event){
                $(event.currentTarget).addClass('hidden');
                //获取动态资讯
                var data = {
                    infoType:'1' ,
                    pageSize:10, pageNum:1,
                    keyword:"",
                    element: n.flashBlock
                };
                this.oldTimestamp = new Date().getTime();
                getQuikeListIndex(data);
            },
            // 涨跌切换
            changeUpDown:function(event){
                //  0 涨    1 跌
                $('.btn-change').removeClass('btn-active');
                $(event.currentTarget).addClass('btn-active');
                var btnValue = $(event.currentTarget).attr('value');
                // var currValue = $(event.currentTarget).parent().parent().find('li.active').eq(0).attr('value');
                this.valueType = btnValue;
                // 查询涨跌幅榜
                var data = {
                        timeType : '1d',
                        valueType : this.valueType,
                        element: this,
                };
                getSummaryChange(data);

            },
            changeSort: function(event){
                var sortType = event.currentTarget.getAttribute('value'); //降序1 升序 0


                if(sortType == '0'){
                    //降序排列
                    event.currentTarget.setAttribute('value','1');
                    event.currentTarget.setAttribute('src','image/paixu2.png');
                    var flowBlock = oClone(this.flowBlock);
                    Vue.set(this,'flowBlock',[]);
                    var temp = null;
                    for(var i=0; i<flowBlock.length; i++ ){
                        //比较字段  usd
                        temp = oClone(flowBlock[i]);
                        for(var j=i+1; j<flowBlock.length; j++ ){
                            if(flowBlock[i].usdOld < flowBlock[j].usdOld){
                                flowBlock[i] = oClone(flowBlock[j]);
                                flowBlock[j] = oClone(temp);
                                temp = oClone(flowBlock[i]) ;
                            }
                        }
                        Vue.set(this.flowBlock, this.flowBlock.length,temp);
                    }
                }else{
                    //升序排列
                    event.currentTarget.setAttribute('value','0');
                    event.currentTarget.setAttribute('src','image/paixu1.png');
                    var flowBlock = oClone(this.flowBlock);
                    Vue.set(this,'flowBlock',[]);
                    var temp = null;
                    for(var i=0; i<flowBlock.length; i++ ){
                        //比较字段  usd
                        temp = oClone(flowBlock[i]);
                        for(var j=i+1; j<flowBlock.length; j++ ){
                            if(flowBlock[i].usdOld > flowBlock[j].usdOld){
                                flowBlock[i] = oClone(flowBlock[j]);
                                flowBlock[j] = oClone(temp);
                                temp = oClone(flowBlock[i]) ;
                            }
                        }
                        Vue.set(this.flowBlock, this.flowBlock.length,temp);
                    }
                }
            },
        },
        mounted:function(){
            this.$nextTick(function(){
                // 查询快讯
                //获取动态资讯
                var data = {
                    infoType:'1' ,
                    pageSize:10, pageNum:1,
                    keyword:"",
                    element: n
                };
                n.oldTimestamp = new Date().getTime();
                getQuikeListIndex(data);

                //24H涨跌幅  0涨   1跌
                getSummaryChange({timeType: '1d',valueType:0, element: this});   //	timeType时间类型	是	String :	1h 1小时   1d 1天    7d 7天    30d 30天

                // 查询资金流入榜
                var directionType = true? 1:0; //流向
                getSummaryFlow({timeType: '1d',directionType:this.directionType, element: this});


                // 查询交易所公告
                getBourList({pageSize:this.pageSize,pageNum :this.pageNum, element: this});



            });
        },
    });



    //增加标记
    // addCalenderMark( { '2018-7-10':'XRR','2018-7-11':'XRR1','2018-7-8': 'BTC','2018-7-12':'XRR2'});
    // addCalenderMark( { '2018-7-9':'XRR---newCurrency.html', '2018-7-31':'BTC---newCurrency.html','2018-8-10':'COX---newCurrency.html'});
    var today = new Date();
    function getMonthDays(month,year){
        var day = "";
        switch(parseInt(month)){
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                day=31;
                break;
            case 2:
                if(getRunNian(parseInt(year))){
                    day = 28;
                }else{
                    day = 29;
                }
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                day = 30;
                break;
        }
        return day;
    }

    var myCalender = new SimpleCalendar('#newBlock');
    var params = {
        startDate: today.getFullYear()+addZero(today.getMonth()+1)+'01', //	起始日期	是	8	String	yyyymmdd日期格式
        endDate:today.getFullYear()+addZero(today.getMonth()+1)+getMonthDays(today.getMonth()+1,today.getFullYear()), //	截止日期	是	8	String	yyyymmdd日期格式，时间跨度不超过1个月
        element: myCalender,
    };
    getCoinNewArea(params);

    //日历上一个月
    $('.online-box').on('click','.sc-mleft',function(){
        var year = $('.sc-select-year').val();
        var month = $('.sc-select-month').val();

        var params = {
            startDate: year+addZero(month)+'01', //	起始日期	是	8	String	yyyymmdd日期格式
            endDate:today.getFullYear()+addZero(month)+getMonthDays(month,year), //	截止日期	是	8	String	yyyymmdd日期格式，时间跨度不超过1个月
            element: myCalender,
        };
        getCoinNewArea(params);
    });




    //左右箭头,滑动函数
    // $('.next-btn').on('click',{sign:'next'},blockSilder);
    // $('.prev-btn').on('click',{sign:'prev'},blockSilder);
    //
    // //滑动函数，e为传入参数
    // function blockSilder(e){
    //     var obj = $(this).nextAll('.item-group').eq(0);
    //     var width = parseFloat(obj.width());
    //     if(e.data.sign == 'next'){
    //         var left = parseFloat(obj.children().css('left')) - width;
    //         if(Math.abs(left) < obj.children().width()){
    //             obj.find('.item-box').eq(0).animate({left: left},500);
    //         }else{
    //             obj.find('.item-box').eq(0).animate({left: 0},500);
    //         }
    //     }else{
    //         var left = parseFloat(obj.children().css('left')) + width;
    //         if(left <=0){
    //             obj.find('.item-box').eq(0).animate({left: left},500);
    //         }
    //     }
    // }
    //
    //
    // //计算滚动内容的宽度；
    // function setTuyereWidth() {
    //     var boxWidth = $('.china-item').eq(0).width();
    //     var tuyereWidth = Math.ceil(c.tuyereBlock.length / 3) * boxWidth;
    //     $('#tuyere').find('.item-box').width(tuyereWidth);
    // }
    // //计算滚动内容的宽度；
    // function setOtherWidth() {
    //     var boxWidth = $('.china-item').eq(0).width();
    //     var buyWidth = Math.ceil(c.lists[1].block.length / 3) * boxWidth;
    //     var performanceWidth = Math.ceil(c.lists[0].block.length / 3) * boxWidth;
    //     var sestemWidth = Math.ceil(c.lists[2].block.length / 3) * boxWidth;
    //     $('#every-buy').find('.item-box').width(buyWidth);
    //     $('#performance').find('.item-box').width(performanceWidth);
    //     $('#sestem').find('.item-box').width(sestemWidth);
    // }


});